package com.jinxin.platform.cdfaceacs.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jinxin.platform.cdfaceacs.mapper.CdattendancerecordRecordMapper;
import com.jinxin.platform.cdfaceacs.pojo.domains.CdattendancerecordRecord;
import com.jinxin.platform.cdfaceacs.pojo.domains.CdattendancerecordUser;
import com.jinxin.platform.cdfaceacs.pojo.dto.UserMonthDto;
import com.jinxin.platform.cdfaceacs.pojo.form.AppMonthForm;
import com.jinxin.platform.cdfaceacs.pojo.form.AppPageForm;
import com.jinxin.platform.cdfaceacs.pojo.form.RecordPageForm;
import com.jinxin.platform.cdfaceacs.service.CdattendancerecordRecordService;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 考勤记录模块-记录汇总表(CdattendancerecordRecord)表服务实现类
 *
 * @author yangjie
 * @since 2021-04-23 10:03:09
 */
@Service("cdattendancerecordRecordService")
public class CdattendancerecordRecordServiceImpl implements CdattendancerecordRecordService {
    @Resource
    private CdattendancerecordRecordMapper cdattendancerecordRecordMapper;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public CdattendancerecordRecord queryById(String id) {
        return this.cdattendancerecordRecordMapper.queryById(id);
    }

    /**
     * 查询多条数据
     *
     * @param cdattendancerecordRecord 实例对象
     * @return 对象列表
     */
    @Override
    public List<CdattendancerecordRecord> queryAll(CdattendancerecordRecord cdattendancerecordRecord) {
        return this.cdattendancerecordRecordMapper.queryAll(cdattendancerecordRecord);
    }

    /**
     * 新增数据
     *
     * @param cdattendancerecordRecord 实例对象
     * @return 实例对象
     */
    @Override
    public CdattendancerecordRecord insert(CdattendancerecordRecord cdattendancerecordRecord) {
        this.cdattendancerecordRecordMapper.insert(cdattendancerecordRecord);
        return cdattendancerecordRecord;
    }

    /**
     * 修改数据
     *
     * @param cdattendancerecordRecord 实例对象
     * @return 实例对象
     */
    @Override
    public CdattendancerecordRecord update(CdattendancerecordRecord cdattendancerecordRecord) {
        this.cdattendancerecordRecordMapper.update(cdattendancerecordRecord);
        return this.queryById(cdattendancerecordRecord.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(String id) {
        return this.cdattendancerecordRecordMapper.deleteById(id) > 0;
    }

    @Override
    public PageInfo<CdattendancerecordRecord> appPageRecordList(AppPageForm pageForm) {
        Page<CdattendancerecordRecord> page = PageHelper.startPage(pageForm.getPageNum(), pageForm.getPageSize());
        PageInfo<CdattendancerecordRecord> pageInfo = new PageInfo<>(cdattendancerecordRecordMapper.pageRecordList(pageForm));
        pageInfo.setPages(page.getPages());
        pageInfo.setTotal(page.getTotal());
        return pageInfo;
    }

    @Override
    public PageInfo<CdattendancerecordRecord> webPageRecordList(RecordPageForm pageForm) {
        Page<CdattendancerecordRecord> page = PageHelper.startPage(pageForm.getPageNum(), pageForm.getPageSize());
        PageInfo<CdattendancerecordRecord> pageInfo = new PageInfo<>(cdattendancerecordRecordMapper.webPageRecordList(pageForm));
        pageInfo.setPages(page.getPages());
        pageInfo.setTotal(page.getTotal());
        return pageInfo;
    }

    @Override
    public List<CdattendancerecordRecord> getByMonth(AppMonthForm monthForm) {
        return cdattendancerecordRecordMapper.getByMonth(monthForm);
    }

    @Override
    public CdattendancerecordRecord getByWorkNoAndDate(String workNo, Date nowDate) {
        return cdattendancerecordRecordMapper.getByWorkNoAndDate(workNo, nowDate);
    }

    @Override
    public List<CdattendancerecordRecord> getTodayRecord() {
        return cdattendancerecordRecordMapper.getTodayRecord();
    }

    @Override
    public void updateUserName(CdattendancerecordUser result) {
        if (!StringUtils.isEmpty(result.getUserName())
                && !StringUtils.isEmpty(result.getWorkNo())) {
            cdattendancerecordRecordMapper.updateUserName(result);
        }
        if (!StringUtils.isEmpty(result.getOrgName())
                && !StringUtils.isEmpty(result.getWorkNo())) {
            cdattendancerecordRecordMapper.updateOrgName(result);
        }
    }

    @Override
    public PageInfo<UserMonthDto> pageUserByMonthList(RecordPageForm pageForm) {
        Page<UserMonthDto> page = PageHelper.startPage(pageForm.getPageNum(), pageForm.getPageSize());
        List<UserMonthDto> userMonthList = cdattendancerecordRecordMapper.pageUserByMonthList(pageForm);
        if (userMonthList != null && userMonthList.size() > 0) {
            for (UserMonthDto userMonthDto : userMonthList) {
                List<CdattendancerecordRecord> byWorkNoAndMonth = cdattendancerecordRecordMapper.getByWorkNoAndMonth(userMonthDto.getWorkNo(), pageForm.getDate());
                userMonthDto.setRecordList(byWorkNoAndMonth);
            }
        }
        if (userMonthList == null) {
            userMonthList = new ArrayList<>();
        }
        PageInfo<UserMonthDto> pageInfo = new PageInfo<>(userMonthList);
        pageInfo.setPages(page.getPages());
        pageInfo.setTotal(page.getTotal());
        return pageInfo;
    }

    @Override
    public List<Map> countByClockStatus() {
        return cdattendancerecordRecordMapper.countByClockStatus();
    }
}
